//
//  GaAlertManager.m
//  CameraLiveDemo
//
//  Created by Gikki Ares on 2020/6/12.
//

#import "JsAlertManager.h"

@implementation JsAlertManager

+ (UIAlertController *)confirmAlertWithMessage:(NSString *)message {
    UIAlertController * ac = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
    [ac addAction:action];
    return ac;
}

+ (void)showConfirmAlertAtCurrentViewControllerWithMessage:(NSString *)message  {
	UIAlertController * ac = [self confirmAlertWithMessage:message];
	UIViewController * vc = [[UIApplication sharedApplication].windows firstObject].rootViewController;
	[vc presentViewController:ac animated:YES completion:nil];
}

+ (UIViewController *)topViewController {
	UIViewController * vc = [[UIApplication sharedApplication].windows firstObject].rootViewController;
	while(vc.presentedViewController) {
		vc = vc.presentedViewController;
	}
	return vc;
}

+ (void)showAlert:(UIAlertController *)ac {
	UIViewController * luiViewController = [self topViewController];
	[luiViewController presentViewController:ac animated:YES completion:nil];
}

+ (UIAlertController *)textFieldAlertWithTitle:(NSString *)title message:(NSString *)message completion:(void(^)(NSString * context))completion {
	UIAlertController * ac = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
		//在AlertView中添加一个输入框
	[ac addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
		textField.placeholder = @"视频标题";
	}];
	UIAlertAction * action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		if(completion) {
			UITextField * tf = ac.textFields.firstObject;
			completion(tf.text);
		}
	}];
	[ac addAction:action];
	return ac;
}


+ (UIAlertController *)textFieldAlertWithTitle:(NSString *)title message:(NSString *)message placeholderArray:(NSArray *)placeholderArray defaultValueArray:(NSArray *)defaultValueArray confirmHandler:(void(^)(NSArray * arr))confirmHandler {
	int textFieldCount = (int)placeholderArray.count;
	UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
	for(int i = 0;i<textFieldCount;i++) {
		[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
			NSString * placeholder = placeholderArray[i];
			textField.placeholder = placeholder;
			NSString * value = defaultValueArray[i];
			if(value && value.length > 0) {
				textField.text = value;
			}
		}];
	}
	[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
	[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		
		if(confirmHandler) {
			NSMutableArray * marr = [NSMutableArray array];
			for(int i = 0;i<textFieldCount;i++) {
				[marr addObject:alertController.textFields[i].text];
			}
			confirmHandler(marr);
		}
	}]];
	return alertController;
}


@end
